package com.mapforge.gui.tools;

import java.util.Iterator;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import com.mapforge.listener.TileLayerSelectionEvent;
import com.mapforge.listener.TileLayerSelectionListener;
import com.mapforge.listener.TileWriterChangeEvent;
import com.mapforge.listener.TileWriterChangeListener;
import com.mapforge.map.TileLayer;
import com.mapforge.map.tools.selection.AbstractSelectionTool;

public class SelectionToolButton extends MapToolButton implements
		TileWriterChangeListener, TileLayerSelectionListener {

	/**
	 * Default serial version ID, here to remove warnings.
	 */
	private static final long serialVersionUID = 1L;

	public SelectionToolButton(ImageIcon icon, AbstractSelectionTool mapTool) {
		super(icon, mapTool, (JPanel) (new SelectionToolControlPanel(mapTool)));
	}

	public AbstractSelectionTool getSelectionTool() {
		return (AbstractSelectionTool) this.getMapTool();
	}

	@Override
	public void tileWriterChangePerformed(TileWriterChangeEvent event) {
		getSelectionTool().setTileWriter(event.getTileWriter());
	}
	
//	@Override
//	public void valueChanged(ListSelectionEvent e) {
//		Object obj = e.getSource();
//		if (obj instanceof TileLayerTransferableContainer) {
//			TileLayerTransferableContainer con = (TileLayerTransferableContainer) obj;
//			if (con.getNumberSelectedPanels() == 1) {
//				Iterator<TransferablePanel> it = con.getSelectedPanels();
//				TransferablePanel panel = it.next();
//				if (panel instanceof TileLayerTransferablePanel) {
//					TileLayer layer = ((TileLayerTransferablePanel) panel)
//							.getTileLayer();
//					getSelectionTool().setTileLayer(layer);
//					System.out.println(this.getMapTool().getDescription()
//							+ " Set tile layer layer to " + layer.getName());
//				} else {
//					getSelectionTool().setTileLayer(null);
//				}
//			} else {
//				getSelectionTool().setTileLayer(null);
//			}
//		}
//	}

	@Override
	public void tileLayerSelectionPerformed(TileLayerSelectionEvent event) {
		if(event.getNumberLayersSelected()==1){
			Iterator<TileLayer> it = event.iterator();
			getSelectionTool().setTileLayer(it.next());
		}else{
			getSelectionTool().setTileLayer(null);
		}
	}
}
